/*
 * Created by JFormDesigner on Tue Nov 10 13:44:46 CET 2009
 */

package at.jku.pervasive.gui.jformdesigner;

import java.awt.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.swing.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import at.jku.pervasive.Application;
import at.jku.pervasive.ApplicationListener;
import at.jku.pervasive.GestureListener;
import at.jku.pervasive.Measurement;
import at.jku.pervasive.Position;
import at.jku.pervasive.PositionListener;
import at.jku.pervasive.TagPosition;
import at.jku.pervasive.TagPositionListener;
import at.jku.pervasive.gui.View3DPanel;
import at.jku.pervasive.gui.action.ExitAction;
import at.jku.pervasive.gui.action.LoadMeasurementAction;
import at.jku.pervasive.gui.action.PreferenceAction;
import at.jku.pervasive.gui.action.ResetViewAction;
import at.jku.pervasive.recognition.Gesture;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.*;

/**
 * @author SHOCKIE
 */
public class UbisenseApplicationFrame extends JFrame implements TagPositionListener, ApplicationListener,
		GestureListener
{

	private boolean updateView3DOnPositionChange = true;
	private boolean listPositionsInTable;

	public UbisenseApplicationFrame()
	{
		initComponents();

		recordingPanel.setTagAssigmentPanel(tagAssignmentPanel);

		loadMeasurementMenuItem.setAction(new LoadMeasurementAction(this));
		preferencesMenuItem.setAction(new PreferenceAction(this));
		exitMenuItem.setAction(new ExitAction(this));

		resetViewButton.setAction(new ResetViewAction(view3DPanel));
		
		replayMeasurementPanel.setTagAssignmentPanel(tagAssignmentPanel);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		pack();
		validate();

		Application app = Application.getInstance();

		app.addApplicationListener(this);
		app.addTagPositionListener(this);
		app.addGestureListener(this);

		app.startListening();
	}

	private void initComponents()
	{
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Open Source Project license - unknown
		mainMenuBar = new JMenuBar();
		JMenu fileMenu = new JMenu();
		loadMeasurementMenuItem = new JMenuItem();
		preferencesMenuItem = new JMenuItem();
		exitMenuItem = new JMenuItem();
		loggingPanel = new JPanel();
		JTabbedPane tagsTabbedPane = new JTabbedPane();
		availableTagsPanel = new AvailableTagsPanel();
		tagAssignmentPanel = new TagAssignmentPanel();
		measurementTabbedPane = new JTabbedPane();
		recordingPanel = new RecordingPanel();
		replayMeasurementPanel = new ReplayMeasurementPanel();
		statusTextField = new JTextField();
		JPanel viewPanel = new JPanel();
		gestureLabel = new JLabel();
		JPanel resetViewButtonPanel = new JPanel();
		resetViewButton = new JButton();
		view3DPanel = new View3DPanel();
		CellConstraints cc = new CellConstraints();

		//======== this ========
		setName("this");
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== mainMenuBar ========
		{
			mainMenuBar.setName("mainMenuBar");

			//======== fileMenu ========
			{
				fileMenu.setText("File");
				fileMenu.setName("fileMenu");

				//---- loadMeasurementMenuItem ----
				loadMeasurementMenuItem.setText("Load measurement ...");
				loadMeasurementMenuItem.setName("loadMeasurementMenuItem");
				fileMenu.add(loadMeasurementMenuItem);

				//---- preferencesMenuItem ----
				preferencesMenuItem.setText("Preferences ...");
				preferencesMenuItem.setName("preferencesMenuItem");
				fileMenu.add(preferencesMenuItem);
				fileMenu.addSeparator();

				//---- exitMenuItem ----
				exitMenuItem.setText("Exit");
				exitMenuItem.setName("exitMenuItem");
				fileMenu.add(exitMenuItem);
			}
			mainMenuBar.add(fileMenu);
		}
		setJMenuBar(mainMenuBar);

		//======== loggingPanel ========
		{
			loggingPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
			loggingPanel.setName("loggingPanel");
			loggingPanel.setLayout(new FormLayout(
				ColumnSpec.decodeSpecs("max(default;211dlu):grow"),
				new RowSpec[] {
					new RowSpec("top:max(default;90dlu)"),
					FormFactory.LINE_GAP_ROWSPEC,
					new RowSpec("top:max(default;75dlu):grow")
				}));

			//======== tagsTabbedPane ========
			{
				tagsTabbedPane.setName("tagsTabbedPane");

				//---- availableTagsPanel ----
				availableTagsPanel.setName("availableTagsPanel");
				tagsTabbedPane.addTab("Available tags", availableTagsPanel);


				//---- tagAssignmentPanel ----
				tagAssignmentPanel.setName("tagAssignmentPanel");
				tagsTabbedPane.addTab("Tag assignments", tagAssignmentPanel);

			}
			loggingPanel.add(tagsTabbedPane, cc.xywh(1, 1, 1, 1, CellConstraints.DEFAULT, CellConstraints.TOP));

			//======== measurementTabbedPane ========
			{
				measurementTabbedPane.setName("measurementTabbedPane");

				//---- recordingPanel ----
				recordingPanel.setName("recordingPanel");
				measurementTabbedPane.addTab("Recording", recordingPanel);


				//---- replayMeasurementPanel ----
				replayMeasurementPanel.setName("replayMeasurementPanel");
				measurementTabbedPane.addTab("Play recording", replayMeasurementPanel);

			}
			loggingPanel.add(measurementTabbedPane, cc.xywh(1, 3, 1, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
		}
		contentPane.add(loggingPanel, BorderLayout.WEST);

		//---- statusTextField ----
		statusTextField.setEditable(false);
		statusTextField.setName("statusTextField");
		contentPane.add(statusTextField, BorderLayout.SOUTH);

		//======== viewPanel ========
		{
			viewPanel.setName("viewPanel");
			viewPanel.setLayout(new BorderLayout());

			//---- gestureLabel ----
			gestureLabel.setText(" ");
			gestureLabel.setHorizontalAlignment(SwingConstants.CENTER);
			gestureLabel.setForeground(Color.red);
			gestureLabel.setFont(gestureLabel.getFont().deriveFont(gestureLabel.getFont().getStyle() | Font.BOLD, gestureLabel.getFont().getSize() + 10f));
			gestureLabel.setName("gestureLabel");
			viewPanel.add(gestureLabel, BorderLayout.NORTH);

			//======== resetViewButtonPanel ========
			{
				resetViewButtonPanel.setBorder(new EmptyBorder(0, 15, 0, 15));
				resetViewButtonPanel.setName("resetViewButtonPanel");
				resetViewButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

				//---- resetViewButton ----
				resetViewButton.setText("text");
				resetViewButton.setName("resetViewButton");
				resetViewButtonPanel.add(resetViewButton);
			}
			viewPanel.add(resetViewButtonPanel, BorderLayout.SOUTH);

			//---- view3DPanel ----
			view3DPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
			view3DPanel.setName("view3DPanel");
			viewPanel.add(view3DPanel, BorderLayout.CENTER);
		}
		contentPane.add(viewPanel, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(null);
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Open Source Project license - unknown
	private JMenuBar mainMenuBar;
	private JMenuItem loadMeasurementMenuItem;
	private JMenuItem preferencesMenuItem;
	private JMenuItem exitMenuItem;
	private JPanel loggingPanel;
	private AvailableTagsPanel availableTagsPanel;
	private TagAssignmentPanel tagAssignmentPanel;
	private JTabbedPane measurementTabbedPane;
	private RecordingPanel recordingPanel;
	private ReplayMeasurementPanel replayMeasurementPanel;
	private JTextField statusTextField;
	private JLabel gestureLabel;
	private JButton resetViewButton;
	private View3DPanel view3DPanel;
	// JFormDesigner - End of variables declaration //GEN-END:variables

	public void setUpdateView3D(boolean update)
	{
		this.updateView3DOnPositionChange = update;
		this.view3DPanel.clearAllPointsForTagIds();
	}

	public void startedTracingTag(String tagId)
	{
		statusTextField.setText("started tracing tag with id " + tagId);
	}

	public void stoppedTracingTag(String tagId)
	{
		statusTextField.setText("stopped tracing tag with id " + tagId);
		view3DPanel.removePointForTagId(tagId);
	}

	public void setStatusText(String text)
	{
		statusTextField.setText(text);
	}

	public void startedListening(String host, int port)
	{
		statusTextField.setText("started listening at " + host + ":" + port);
	}

	public void stoppedListening(String host, int port)
	{
		statusTextField.setText("stopped listening at " + host + ":" + port);
	}

	public String getMeasurementDescription()
	{
		return recordingPanel.getDescription();
	}

	public void showTagPositions(Map<String, List<Position>> tagPositions)
	{
		view3DPanel.showTagPositions(tagPositions);
	}

	public void setLoadedMeasurement(Measurement measurement)
	{
		recordingPanel.setLoadedMeasurement(measurement);
		tagAssignmentPanel.setLoadedMeasurement(measurement);
	}

	@Override
	public void tagAssignmentChanged(TagPosition tagPosition, String tagId)
	{
		// update the view3DPanel
		view3DPanel.tagAssignmentChanged(tagPosition, tagId);
	}

	@Override
	public void tagPositionChanged(TagPosition tagPosition, Position newPosition)
	{
		view3DPanel.updateTagPosition(tagPosition, newPosition);
	}

	@Override
	public void gestureRecognized(Gesture gesture, Date dateStarted)
	{
		gestureLabel.setText(gesture == Gesture.NONE ? " " : gesture.toString());
	}

	private class View2DPanel extends JPanel {
		private View2DPanel() {
			initComponents();
		}

		private void initComponents() {
			// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
			// Generated using JFormDesigner Open Source Project license - unknown
			// JFormDesigner - End of component initialization  //GEN-END:initComponents
		}

		// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
		// Generated using JFormDesigner Open Source Project license - unknown
		// JFormDesigner - End of variables declaration  //GEN-END:variables
	}
}
